package com.djinfo.work.service.impl;

import com.djinfo.work.common.service.impl.BaseServiceImpl;
import com.djinfo.work.common.util.StringUtil;
import com.djinfo.work.dao.HospitalUserDao;
import com.djinfo.work.dao.impl.HospitalUserDaoImpl;
import com.djinfo.work.entity.QsDictHospital;
import com.djinfo.work.entity.QsHospitalUserEntity;
import com.djinfo.work.entity.TbSysUsers;
import com.djinfo.work.service.HospitalUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by admin on 2017/7/25.
 */
@Service("HospitalUserService")
public class HospitalUserServiceImpl extends BaseServiceImpl<QsHospitalUserEntity,HospitalUserDaoImpl> implements HospitalUserService {

    @Autowired
    private HospitalUserDao hospitalUserDao;


    @Override
    public QsHospitalUserEntity getQsHospitalUserEntityByHid(String hid) {
        QsHospitalUserEntity qsHospitalUserEntity=new QsHospitalUserEntity();
        //查出该QsHospitalUserEntity的销售人员
        String hql="from QsHospitalUserEntity where hid=? and roleType=1";
        List<QsHospitalUserEntity> list=hospitalUserDao.getList(hql,hid);
        if(list.size()!=0){
            qsHospitalUserEntity=list.get(0);
        }

        return qsHospitalUserEntity;
    }

    @Override
    public void insert() {

        String nowTime= StringUtil.getNowTime();


        String hql="from TbSysUsers";
        String hql2="from QsDictHospital";

        List<QsDictHospital> hospitals=hospitalUserDao.getList(hql2);

        List<TbSysUsers> userList=hospitalUserDao.getList(hql);
            for(QsDictHospital qsDictHospital:hospitals){
                for(TbSysUsers user:userList){
                    if(user.getRealName().equals(qsDictHospital.getSales())){
                        QsHospitalUserEntity qsHospitalUserEntity=new QsHospitalUserEntity();
                        qsHospitalUserEntity.setHid(qsDictHospital.getId());
                        qsHospitalUserEntity.setRoleType(1);
                        qsHospitalUserEntity.setUid(user.getPkUserId());
                        hospitalUserDao.save(qsHospitalUserEntity);
                    }
                    if(user.getRealName().equals(qsDictHospital.getZsm())){
                        QsHospitalUserEntity qsHospitalUserEntity=new QsHospitalUserEntity();
                        qsHospitalUserEntity.setHid(qsDictHospital.getId());
                        qsHospitalUserEntity.setRoleType(2);
                        qsHospitalUserEntity.setUid(user.getPkUserId());
                        hospitalUserDao.save(qsHospitalUserEntity);
                    }
                    if(user.getRealName().equals(qsDictHospital.getRsm())){
                        QsHospitalUserEntity qsHospitalUserEntity=new QsHospitalUserEntity();
                        qsHospitalUserEntity.setHid(qsDictHospital.getId());
                        qsHospitalUserEntity.setRoleType(3);
                        qsHospitalUserEntity.setUid(user.getPkUserId());
                        hospitalUserDao.save(qsHospitalUserEntity);
                    }
            }


        }




    }
}
